package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.CoachMapper;
import com.cqjtu.drive.mapper.CourseMapper;
import com.cqjtu.drive.mapper.UserMapper;
import com.cqjtu.drive.pojo.Coach;
import com.cqjtu.drive.pojo.Course;
import com.cqjtu.drive.pojo.User;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.CoachService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CoachServiceImpl implements CoachService {

    @Autowired
    private CoachMapper coachMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public Response<Coach> getCoachById(String coachId) {
        Coach coach = coachMapper.getCoachById(coachId);
        if (coach != null) {
            return new Response<>(true, "Coach found", coach);
        } else {
            return new Response<>(false, "Coach not found");
        }
    }

    @Override
    public Response<Coach> getCoachByUserId(String userId) {
        Coach coach = coachMapper.getCoachByUserId(userId);
        if (coach != null) {
            return new Response<>(true, "Coach found", coach);
        } else {
            return new Response<>(false, "Coach not found");
        }
    }

    @Override
    public Response<String> insertCoach(Coach coach) {
        try {
            coachMapper.insertCoach(coach);
            return new Response<>(true, "Coach inserted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to insert coach: " + e.getMessage());
        }
    }

    @Override
    public Response<String> updateCoach(Coach coach) {
        try {
            coachMapper.updateCoach(coach);
            return new Response<>(true, "Coach updated successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to update coach: " + e.getMessage());
        }
    }

    @Override
    public Response<String> deleteCoach(String coachId) {
        try {
            Coach coach = coachMapper.getCoachById(coachId);
            userMapper.deleteUser(coach.getUserId());
            coachMapper.deleteCoach(coachId);
            return new Response<>(true, "Coach deleted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to delete coach: " + e.getMessage());
        }
    }

    @Override
    public Response<List<Coach>> getAllCoaches() {
        List<Coach> coaches = coachMapper.getAllCoaches();
        if (coaches != null && !coaches.isEmpty()) {
            return new Response<>(true, "Coaches retrieved successfully", coaches);
        } else {
            return new Response<>(false, "No coaches found");
        }
    }

    @Override
    public Response<List<Coach>> getCoachesByName(String cName) {
        List<Coach> coaches = coachMapper.getCoachesByName(cName);
        if (coaches != null && !coaches.isEmpty()) {
            return new Response<>(true, "Coaches retrieved successfully", coaches);
        } else {
            return new Response<>(false, "No coaches found");
        }    }


}
